home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / demo / igps_102.zip / SOCKET.H < prev    next >
C/C++ Source or Header  |  1994-11-30  |  7KB  |  226 lines

  1. ////////////////////////////////////////////////////////////////////////////////////
  2. // Internet Global Phone Project
  3. // socket.h : definitions of the CSocketOwner base classes                             
  4. //
  5. // See Dr. Dobb's Journal text for a description of the classes.  These C++
  6. // classes attempt to provide a more 'humane' way to program Winsock by shielding
  7. // much of the complexity and providing a higher level abstraction based on
  8. // the client/server/listener paradigm made popular in BSD networking.
  9. //
  10. // Source for implementation of these base classes are not provided as they tie
  11. // closely to the WSNETWRK library.  Link with SOCKLIB.LIB to use these
  12. // classes.
  13. ////////////////////////////////////////////////////////////////////////////////////
  14. // Copyright (c) 1993-1994    microWonders Inc.  All rights reserved.
  15. //                                                                         
  16. // AN OPEN INVITION TO BUILD UPON AND CONTRIBUTE TO THE PUBLIC TECHNOLOGY POOL:
  17. // You are encouraged to redistribute, and build upon the technologies presented 
  18. // in this source module and the accompanying article in Dr. Dobb's Journal provided 
  19. // all the conditions listed in the MUSTREAD.TXT file, included with this 
  20. // distribution, are met.
  21. ////////////////////////////////////////////////////////////////////////////////////
  22.  
  23. #define WS_TALKCLIENT    3
  24. #define WSTALKLISTENER    2  
  25. #define WSTALKCHILD        4
  26.  
  27. #define MIN_UINTERVAL    3000
  28. //#define WSTALKBUFFERSIZE 255
  29.  
  30. #define WSTALKFAILED    1   
  31. #define WSTALKLISFAILED    6       
  32. #define WSTALKBACK        0
  33. #define WSTALKBEGIN        0x0300
  34. #define WSCHILDCLOSED    7 
  35. #define WSTALKLISCONNECT 8
  36. #define WSTALKNEWCHILD    5
  37.  
  38.  
  39. #define TCP_PROTOCOL    "tcp"   
  40. #define TALK_SERVICE    "intertalker"
  41.  
  42. // legacy construct for prototype of echo server
  43. //
  44. typedef struct
  45. {
  46.   WORD  seq;
  47.   DWORD time;
  48.   DWORD done;
  49.   char  szBuffer[80];
  50. } ECHOPACKET, near *PECHOPACKET, far *LPECHOPACKET;
  51.  
  52. #define WSTALKBUFFERSIZE sizeof(ECHOPACKET)  
  53.  
  54. void __export WINAPI UniversalCSockOwnerNotify(WSSOCKET iEntry, int iEvent, int iError);
  55.  
  56. class CSocketOwner : public CObject
  57. {
  58. public: 
  59.     CSocketOwner() {}           
  60.     ~CSocketOwner() {} 
  61.     WSSOCKET GetSafeSocket() const {return (m_socket); } 
  62.     BOOL SetSafeSocket(WSSOCKET sock) {m_socket = sock; return TRUE;}
  63.     void Notify(WSSOCKET, int, int);
  64.     virtual void OnConnected(int, int) {}
  65.     virtual void OnSendCompleted(int, int) {}
  66.     virtual void OnReceiveCompleted(int, int) {}
  67.     virtual void OnTimerExpired(int, int)  {}
  68.     virtual void OnDisconnected(int, int)  {}
  69.     virtual void OnOtherMsgs(int, int)  {}     
  70.  
  71. public:  
  72.   int         m_iType;  
  73.   NOTIFYPROC  m_fNotifyProc;  
  74. protected:
  75.     WSSOCKET m_socket;
  76. protected:                                      
  77.     // wrap the WSNETWRK library
  78.     BOOL Accept(LPWSADDRESS addr, LPWSPORTID portid);
  79.     void CallBack(int i, int j);
  80.     BOOL CancelIO(void);
  81.      BOOL  wConnect(LPCSTR lp, WSPORTID portid);
  82.     BOOL ConnectTo(WSADDRESS waddr, WSPORTID portid);
  83.     WSSOCKET CreateSocket(int i, int j, WSPORTID portid, UINT k, HWND hWnd, NOTIFYPROC lpProc);
  84.     BOOL   CreateTimer(UINT i);
  85. public:        
  86.     BOOL DestroySocket(void);
  87. public:
  88.     LPVOID   GetSockMemory(void);
  89. protected:        
  90.     BOOL   KillTimer(void);
  91.     int  LastRecvLength(void);
  92.     HWND  MyCallBack(void);
  93.     int   MyLastError(void);
  94.     BOOL   Recv( LPSTR lp, int i);
  95.     BOOL   RecvFrom( LPSTR lp, int i, LPWSADDRESS lpaddr, LPWSPORTID lpid);
  96.     BOOL Send( LPCSTR lp1, int i);
  97.     BOOL SendTo( LPCSTR lp1, int i, WSADDRESS waddr, WSPORTID portid);
  98. public:                        
  99.     WSADDRESS MyAddress(void);
  100.     // this can also be used to create new server socket... via listener 
  101.     WSSOCKET AttachSocket(HSOCK hSock, UINT i, HWND hWnd, NOTIFYPROC lpf);
  102.     int Checksum(unsigned short far * fp, int i);
  103.     WSADDRESS GetHostByName(LPCSTR name);
  104.     int  GetHostName(LPSTR lp , int i);
  105.        int GetProtocolByName(LPCSTR lp);
  106.     WSPORTID   GetServiceByName(LPCSTR lp1, LPCSTR lp2);
  107.     int  GetServiceByPort(WSPORTID portid, LPCSTR lp1, LPSTR lp2, int i);
  108.     long HToNL(long ld);
  109.     long      NToHL(long lg);
  110.  
  111. // Attributes
  112. public: 
  113. #ifdef _DEBUG
  114.     virtual void AssertValid() const {}
  115.     virtual void Dump(CDumpContext& dc) const {}
  116. #endif
  117. };
  118.  
  119. // NOT USED WITHIN IGP
  120. class CSockClient: public CSocketOwner
  121.  
  122. public: // create from serialization only
  123.     CSockClient(); 
  124.     ~CSockClient(); 
  125.     // overload to connect by name or address
  126.     WSSOCKET Connect( UINT prot, LPCSTR, WSPORTID, UINT, HWND,NOTIFYPROC, UINT);
  127.     WSSOCKET Connect(UINT prot, WSADDRESS, WSPORTID, UINT, HWND, NOTIFYPROC, UINT);
  128.     BOOL SetTimeout(WSSOCKET, UINT);
  129.     BOOL End();
  130.  
  131.     void ResetStats(void); 
  132.     
  133.     virtual void OnConnected(int, int);
  134.     virtual void OnSendCompleted(int, int);
  135.     virtual void OnReceiveCompleted(int, int);
  136.     virtual void OnTimerExpired(int, int);       
  137.     virtual void OnDisconnected(int, int);
  138.     
  139.     void OnSocketSelect();
  140.     void OnTimer();
  141.     void OnCloseSocket();
  142.     
  143.     void SendData();
  144.     void ReadLine();
  145.  
  146.  
  147. private:  
  148.   BOOL        m_bConnected;
  149.   WSADDRESS   m_lAddress;
  150.   WSPORTID    m_uPort;
  151.   WSADDRESS   m_lRecvAddress;
  152.   WSPORTID    m_uRecvPort;
  153.   UINT        m_uAttempts;
  154.   UINT        m_uFailures;
  155.   DWORD       m_dMin;
  156.   DWORD       m_dAvg;
  157.   DWORD       m_dMax;
  158.   long double m_ldTotalTime;
  159.   UINT        m_uSequence;
  160.   BOOL        m_bInProgress;
  161.   UINT        m_uInterval;
  162.   UINT        m_uLateReplies;
  163.   UINT        m_uLimit;
  164.  
  165.   UINT        m_uErrorCount;
  166.   ECHOPACKET  m_Packet;
  167.  
  168. private:       
  169.     void WriteData();
  170.     void DestroyConnection();
  171.     
  172. #ifdef _DEBUG
  173.     virtual void AssertValid() const;
  174.     virtual void Dump(CDumpContext& dc) const;
  175. #endif
  176.  
  177. };
  178.                       
  179. // NOT USED WITHIN IGP                      
  180. class CSockServer: public CSocketOwner
  181. {                                
  182. public:
  183.     CSockServer(); 
  184.     ~CSockServer();
  185.     int Create(void);
  186.     WSSOCKET StartServer(HWND, NOTIFYPROC);
  187.     WSSOCKET EndServer(WSSOCKET);
  188.  
  189.     virtual void OnOtherMsgs(int, int);
  190.     virtual void OnReceiveCompleted(int, int);
  191.     virtual void OnSendCompleted(int, int);
  192.     virtual void OnDisconnected(int, int);
  193.  
  194. private:
  195.   WSPORTID    m_uPort;
  196.   WSADDRESS   m_lAddress;
  197.   char        m_szBuffer[WSTALKBUFFERSIZE];    
  198. #ifdef _DEBUG
  199.     virtual void AssertValid() const;
  200.     virtual void Dump(CDumpContext& dc) const;
  201. #endif
  202.  
  203. }; 
  204.  
  205.  
  206. class CSockListenServer: public CSocketOwner
  207. {                                
  208. public:
  209.     CSockListenServer(); 
  210.     ~CSockListenServer();
  211.     int Create(void);  
  212.     WSSOCKET StartListener(LPCSTR, WSPORTID, LPWSADDRESS,  HWND, NOTIFYPROC);
  213.     BOOL EndListener(); 
  214.  
  215.     virtual void OnOtherMsgs(int, int);
  216.                                              
  217. protected:
  218.  
  219. #ifdef _DEBUG
  220.     virtual void AssertValid() const;
  221.     virtual void Dump(CDumpContext& dc) const;
  222. #endif
  223.     
  224. };
  225.